package com.yuyu.helper;

import com.yuyu.helper.ClassHelper;
import com.yuyu.util.ReflectionUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Bean助手类
 *
 * @author yuyu
 * @date 2018/10/19
 */
public class BeanHelper {

    /**
     * 定义Bean映射(用于存放Bean类与Bean)
     */
    private static final Map<Class<?>, Object> BEAN_MAP = new HashMap<Class<?>, Object>();

    static {
        Set<Class<?>> beanClassSet = ClassHelper.getBeanClassSet();
        for (Class<?> beanClass : beanClassSet) {
            Object obj = ReflectionUtil.newInstance(beanClass);
            BEAN_MAP.put(beanClass, obj);
        }
    }

    /**
     * 获取Bean映射
     */
    public static Map<Class<?>, Object> getBeanMap() {

        return BEAN_MAP;
    }

    public static <T> T getBean(Class<T> cls) {
        if (!BEAN_MAP.containsKey(cls)) {
            throw new RuntimeException("未找到此类对应实体化对象" + cls);
        }
        return (T) BEAN_MAP.get(cls);
    }


}
